﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.BaseChart = function(element) {
    YUIAspNet.BaseChart.initializeBase(this, [element]);
}

YUIAspNet.BaseChart.prototype = {
    initialize: function() {
        YUIAspNet.BaseChart.callBaseMethod(this, 'initialize');
    },

    initializeYUIControl: function(sender, args) {

        if (!this.get_SWFUrl())
            YAHOO.widget.Chart.SWFURL = '<%= WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Chart.charts.swf")%>';
        else
            YAHOO.widget.Chart.SWFURL = this.get_SWFUrl();

        this.initializeChartControl();

        if (this._events) {
            var handler = this._events.getHandler("OnShowTooltip");

            if (handler) {
                var me = this;
                this.chartConfig.dataTipFunction = function(item, index, series) {
                    var arg = { "item": item, "index": index, "series": series };
                    handler(me, arg);
                    return arg.TooltipText;

                };
            }
        }
    },

    _BuildConfig: function() {
        this.chartConfig = { expressInstall: this.get_ExpressInstall() };
        if (this.get_ChartStyle && this.get_ChartStyle())
            this.chartConfig.style = this.get_ChartStyle();

        if (this.get_Polling())
            this.chartConfig.polling = this.get_Polling();

        if (this.get_Series && this.get_Series()) // chart series, if they exist
            this.chartConfig.series = this._BuildSeries();

        if (this.get_YAxis && this.get_YAxis()) // Y Axis, if exists
            this.chartConfig.yAxis = this._getAxisDetail(this.get_YAxis());
        if (this.get_XAxis && this.get_XAxis()) // X Axis, if exists
            this.chartConfig.xAxis = this._getAxisDetail(this.get_XAxis());

        if (this.get_XField && this.get_XField()) // X Field, if exists
            this.chartConfig.xField = this.get_XField();
        if (this.get_YField && this.get_YField()) // Y Field, if exists
            this.chartConfig.yField = this.get_YField();

        return this.chartConfig;
    },

    _BuildSeries: function() {
        var seriesDef = [];
        for (var i = 0; i < this.get_Series().length; i++) {
            var current = this.get_Series()[i];
            var serie = {}; //{type: current.Type};

            if (current.DisplayName)
                serie.displayName = current.DisplayName;

            if (current.YField)
                serie.yField = current.YField;

            if (current.XField)
                serie.xField = current.XField;

            seriesDef.push(serie);
        }

        return seriesDef;
    },

    get_ChartStyle: function() {

        // style is generated on the server...
        if (this.get_Style && this.get_Style())
            return this.get_Style();
        return {};
    },

    _getAxisDetail: function(axis) {
        if (!axis) return null;
        var yuiAxis = null;

        switch (axis.Type) {
            case "numeric":
                yuiAxis = new YAHOO.widget.NumericAxis();
                if (axis.Minimum && axis.Minimum > 0)
                    yuiAxis.minimum = axis.Minimum;
                if (axis.Maximum && axis.Maximum > 0)
                    yuiAxis.maximum = axis.Maximum;
                if (axis.MajorUnit && axis.MajorUnit > 0)
                    yuiAxis.majorUnit = axis.MajorUnit;
                if (axis.MinorUnit && axis.MinorUnit > 0)
                    yuiAxis.minorUnit = axis.MinorUnit;

                if (axis.Format)
                    yuiAxis.labelFunction = function(value) { return String.format(axis.Format, value); }

                yuiAxis.snapToUnits = axis.SnapToUnits;
                yuiAxis.stackingEnabled = axis.EnableStacking;
                yuiAxis.alwaysShowZero = axis.AlwaysShowZero;
                yuiAxis.scale = axis.Scale || "linear";

                break;
            case "category":
                yuiAxis = new YAHOO.widget.CategoryAxis();
                yuiAxis.categoryNames = axis.CategoryNames;
                break;
            default:
                return null;
        }

        return yuiAxis;
    },

    


    add_OnShowTooltip: function(handler) {
        this.get_events().addHandler("OnShowTooltip", handler);
    },
    remove_OnShowTooltip: function(handler) {
        this.get_events().removeHandler("OnShowTooltip", handler);
    },

    get_XAxis: function() {
        return this._xAxis;
    },
    set_XAxis: function(xAxis) {
        this._xAxis = xAxis;
    },

    get_YAxis: function() {
        return this._yAxis;
    },
    set_YAxis: function(yAxis) {
        this._yAxis = yAxis;
    },

    get_Series: function() {
        return this._series;
    },
    set_Series: function(series) {
        this._series = series;
    },

    get_SWFUrl: function() {
        return this._sWFUrl;
    },
    set_SWFUrl: function(sWFUrl) {
        this._sWFUrl = sWFUrl;
    },

    get_Polling: function() {
        return this._polling;
    },
    set_Polling: function(polling) {
        this._polling = polling;
    },

    get_ChartContainer: function() {
        return this._chartContainer;
    },
    set_ChartContainer: function(chartContainer) {
        this._chartContainer = chartContainer;
    },

    get_Style: function() {
        return this._style;
    },
    set_Style: function(style) {
        this._style = style;
    },

    get_XField: function() {
        return this._xField;
    },
    set_XField: function(xField) {
        this._xField = xField;
    },

    get_YField: function() {
        return this._yField;
    },
    set_YField: function(yField) {
        this._yField = yField;
    },

    get_ExpressInstall: function() {
        return '<%= WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.Chart.expressinstall.swf")%>';
    }
    //OnShowTooltip
}

YUIAspNet.BaseChart.registerClass('YUIAspNet.BaseChart', YUIAspNet.BaseControl);
